package com.loyal.loyalerp.humenresource.categoryDetail.api;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.loyal.loyalerp.humenresource.categoryDetail.entity.Gy_ConfigDetail;
import com.loyal.loyalerp.humenresource.categoryDetail.service.CategoryDetailReadService;
import com.loyal.loyalerp.humenresource.categoryDetail.service.CategoryDetailWriteService;


@Controller
@RequestMapping("/categoryDetail")
public class CategoryDetailApiResource {

	@Autowired
	private CategoryDetailReadService detailReadService;
	@Autowired
	private CategoryDetailWriteService detailWreitService;
	
	@RequestMapping("/jsp")
	public String load() {
		return "HumenResource/BaseSetting/categoryDetail";
	}
	
	@RequestMapping("/editjsp")
	public String editjsp() {
		return "HumenResource/BaseSetting/editCategoryDetail";
	}
	
	@ResponseBody
	@RequestMapping("/list")
	public Collection<Gy_ConfigDetail> retrieveCategoryDetailList(int typeId){
		return detailReadService.retrieveCategoryDetail(typeId);
	}
	
	@ResponseBody
	@RequestMapping("/add")
	public Map<String,Object> addConfigDetail(String str) throws JsonParseException, JsonMappingException, IOException{
		Map<String,Object> map=new HashMap<String,Object>();
		
		ObjectMapper mapper = new ObjectMapper();
		String listStr=java.net.URLDecoder.decode(str, "UTF-8");
		Gy_ConfigDetail configDetail = mapper.readValue(listStr, Gy_ConfigDetail.class);
		System.out.println(configDetail.toString());
		int type=detailWreitService.insertDetail(configDetail);
		if(type==1) {
			map.put("status", 200);
		}
		
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/edit")
	public Map<String,Object> editConfigDetail(Gy_ConfigDetail configDetail) throws JsonParseException, JsonMappingException, IOException{
		Map<String,Object> map=new HashMap<String,Object>();
		
		/*ObjectMapper mapper = new ObjectMapper();
		String listStr=java.net.URLDecoder.decode(str, "UTF-8");
		Gy_ConfigDetail configDetail = mapper.readValue(listStr, Gy_ConfigDetail.class);*/
		System.out.println(configDetail.toString());
		int type=detailWreitService.updateDetail(configDetail);
		if(type==1) {
			map.put("status", 200);
		}
		
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Map<String,Object> deleteConfigDetail(int id){
		Map<String,Object> map=new HashMap<String,Object>();
		int type=detailWreitService.deleteDetail(id);
		if(type==1) {
			map.put("status", 200);
		}
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/batchdelete")
	public Map<String,Object> batchConfigDetail(String str) throws JsonParseException, JsonMappingException, IOException{
		Map<String,Object> map=new HashMap<String,Object>();
		ObjectMapper mapper = new ObjectMapper();
		JavaType javaType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Integer.class); 
		String listStr=java.net.URLDecoder.decode(str, "UTF-8");
		@SuppressWarnings("unchecked")
		List<Integer> list=(List<Integer>)mapper.readValue(listStr, javaType);
		int type=detailWreitService.batchDelete(list);
		if(type>=1) {
			map.put("status", 200);
		}
		return map;
	}
}
